const webpack = require('webpack');
const _ = require('lodash');
const pkg = require('./package.json');
const cfg = require('./src/etc');

const LIBS = [
    'react', 'react-dom', 'react-router', 'recompose', 'classnames',
    'redux', 'redux-act', 'react-redux', 'redux-thunk', 'redux-multi',
    'redux-history-transitions', 'updeep',
    'whatwg-fetch', 'jwt-decode', 'moment', 'jquery'
];
const isDev = process.env.NODE_ENV !== 'production';
const libsConfig = isDev
    ? [
    {lib: 'lodash', used: '_', file: 'lodash.js'},
    {lib: 'jquery', used: 'jQuery', file: 'dist/jquery.js'},
    {lib: 'updeep', file: 'dist/umd/updeep-standalone.js'},
    {lib: 'moment', file: ['moment.js', 'locale/zh-cn.js'], min: ['min/moment.min.js', 'locale/zh-cn.js']},
    {lib: 'react', used: 'React', file: 'dist/react-with-addons.js'},
    {lib: 'react-dom', used: 'ReactDOM', file: 'dist/react-dom.js'},
    //{lib: 'react-router', used: 'ReactRouter', file: 'umd/ReactRouter.js'},
    {lib: 'antd', file: ['dist/antd.js', 'dist/antd.css']},
]
    : [];

const libs = _.reduce(libsConfig, (res, i) => {
    const files = _.castArray(isDev ? i.file : (i.min || i.file));
    _.each(files, (v) => {
        let fromName = v;
        let toName = _.last(v.split('/'));
        toName = (isDev || i.min !== undefined && !i.min || !isDev && i.min)
            ? toName :
            _.endsWith(toName, '.js')
                ? (fromName = `${fromName.substr(0, fromName.length - 3)}.min.js`, `${toName.substr(0, toName.length - 3)}.min.js`)
                : _.endsWith(toName, '.css')
                ? (fromName = `${fromName.substr(0, fromName.length - 4)}.min.css`, `${toName.substr(0, toName.length - 4)}.min.css`)
                : toName;
        const url = `/libs/${i.lib}/${pkg.devDependencies[i.lib] || pkg.dependencies[i.lib]}/${toName}`;
        if (_.endsWith(toName, '.js')) {
            res.jses.push(url);
        }
        else if (_.endsWith(toName, '.css')) {
            res.csses.push(url);
        }
        res.copys.push({
            from: `${i.lib}/${fromName}`,
            to: url
        });
    });
    res.externals[i.lib] = i.used || i.lib;
    return res;
}, {externals: {}, copys: [], jses: [], csses: []});

const src = {
    dir: 'src',
    app: {
        dir: 'app',
        main: 'index.js'
    },
    bin: {
        dir: 'bin'
    },
    etc: {
        dir: 'etc'
    },
    web: {
        dir: 'web',
        main: 'index.js',
        libs: LIBS
    },
    res: {
        dir: 'res'
    }
};

const run = {
    dir: 'run',
    web: {
        dir: 'www'
    }
};

const dev = {
    dir: 'dev'
};

const test = {
    dir: 'test'
};

const tmp = {
    dir: 'tmp'
};


const api = {
    path: '/api',
    port: cfg.port
};

module.exports = {
    port: 9000,
    args: ['-c'],
    name: pkg.name,
    main: pkg.main,
    api, src, dev, run, test, tmp, libs,
    plugins: [
        new webpack.ProvidePlugin({
            $: 'jquery',
            jQuery: 'jquery',
            'window.jQuery': 'jquery'
        })
    ],
    proxy: ['js', 'css', 'files', 'tinymce', 'libs']
};
